package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.home_inspection.InspectionBean;

import java.util.ArrayList;
import java.util.List;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;

/**
 * description: 多选
 * created by kalu on 2018/5/19 15:47
 */
public class CheckDialog extends BaseDialog {

    private ArrayList<InspectionBean.DoctorBean> mDatas = new ArrayList();
    private final BaseCommonAdapter<InspectionBean.DoctorBean> adapter = new BaseCommonAdapter<InspectionBean.DoctorBean>(mDatas, R.layout.layout_dialog_check_child) {
        @Override
        protected void onNext(RecyclerHolder holder, InspectionBean.DoctorBean model, int position) {

            final CheckBox box = holder.getView(R.id.dialog_check_box);
            box.setText(model.getReal_name());

            holder.itemView.setOnClickListener(v -> {
                final boolean ischeck = model.isCheck();
                model.setCheck(!ischeck);
            });
        }
    };
    private OnDialogChangeListener listener;

    public CheckDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_check;
    }

    @Override
    protected void initData() {

        findViewById(R.id.dialog_check_left).setOnClickListener(v -> {
            dismiss();
        });
        findViewById(R.id.dialog_check_right).setOnClickListener(v -> {

            ArrayList<InspectionBean.DoctorBean> list = new ArrayList();

            for (InspectionBean.DoctorBean temp : mDatas) {
                if (temp.isCheck()) continue;

                list.add(temp);
            }

            if (null != listener) {
                listener.onChange(list);
            }
            dismiss();
        });

        final RecyclerView recycler = findViewById(R.id.dialog_check_list);
        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        recycler.setLayoutManager(manager);
        recycler.setAdapter(adapter);
    }

    public void setTitle(String str) {

        if (TextUtils.isEmpty(str)) return;
        TextView info = findViewById(R.id.dialog_check_title);
        if (null == info) return;
        info.setText(str);
    }

    public void setButton(String left, String right) {

        TextView info1 = findViewById(R.id.dialog_check_left);
        TextView info2 = findViewById(R.id.dialog_check_right);
        info1.setText(left);
        info2.setText(right);
    }

    public void setList(List<InspectionBean.DoctorBean> list) {
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {

        void onChange(ArrayList<InspectionBean.DoctorBean> list);
    }
}
